package com.mbti.youmore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.mbti.youmore.entity.Follow;
import com.mbti.youmore.entity.User;
import com.mbti.youmore.mapper.FollowMapper;
import com.mbti.youmore.mapper.HistoryMapper;
import com.mbti.youmore.service.UserService;
import com.mbti.youmore.mapper.UserMapper;
import com.mbti.youmore.util.ProvinceMapperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author Guqier
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-05-08 20:30:45
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper; // 使用Spring自动注入工具类

    @Autowired
    private FollowMapper followMapper; // 使用Spring自动注入工具类

    @Autowired
    private HistoryMapper historyMapper; // 使用Spring自动注入工具类
    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
    }

    @Override
    public User getUserByUserId(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public Map<String, Object> getUserByUserIdPlusFollow(Integer userId, Integer targetUserId) throws JsonProcessingException {
        User targetUser = userMapper.selectById(targetUserId);
        if (targetUser == null) return null;

        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.registerModule(new JavaTimeModule());
        // 将pojo对象转换为Map
        String s = objectMapper.writeValueAsString(targetUser);
        Map<String, Object> map = objectMapper.readValue(s, new TypeReference<Map<String, Object>>() {
        });

        // 判断是否关注
        List<Follow> follows = followMapper.selectList(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, userId).eq(Follow::getFollowedUserId, targetUserId));
        if (!follows.isEmpty()) {
            map.put("isFollowed", true);
        } else {
            map.put("isFollowed", false);
        }
        return map;
    }

    @Override
    public List<User> getUserAll() {
        return userMapper.selectList(null);
    }

    @Override
    @Transactional // 添加事务管理器注解
    public boolean updateUser(User user) {
        User targetUser = userMapper.selectById(user.getUserId());
        if (targetUser == null) return false;

        // 将接收到的省份拼音映射为省份名称
        BeanUtils.copyProperties(user, targetUser);
        targetUser.setUserLocation(ProvinceMapperUtil.toName(user.getUserLocation()) != null ? ProvinceMapperUtil.toName(user.getUserLocation()) : user.getUserLocation());
        int count = userMapper.updateById(targetUser);
        return count == 1;
    }

    @Override
    public User getIntegralOne(Integer UserId) {
        userMapper.updateIntegralOneByUserId(UserId);
        return userMapper.selectById(UserId);
    }

    @Override
    public User getIntegralTwo(Integer UserId) {
        userMapper.updateIntegralTwoByUserId(UserId);
        return userMapper.selectById(UserId);
    }

    @Override
    public ResponseEntity<?> signIn(int userId, Date lastTime) {
        User user = userMapper.selectById(userId);
        if (user.canSignInToday()) {
            user.signIn();
            userMapper.updateSiginDateByUserId(userId,lastTime);
            userMapper.updateIntegralOneByUserId(userId);
            return ResponseEntity.ok("Signed in successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("You have already signed in today.");
        }
    }

    @Override
    public ResponseEntity<?> browseIn(int userId, Date lastTime) {
        User user = userMapper.selectById(userId);
        if (user.canBrowseInToday()) {
            user.signIn();
            userMapper.updateBrowseDateByUserId(userId,lastTime);
            userMapper.updateIntegralOneByUserId(userId);
            return ResponseEntity.ok(" obtain browse integral successfully.");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("You have already obtain browse integral today.");
        }
    }

//    @Override
//    public ResponseEntity<?> postIn(int userId, Date lastTime) {
//        User user = userMapper.selectById(userId);
//        if (user.canBrowseInToday()) {
//            user.signIn();
//            userMapper.updateBrowseDateByUserId(userId,lastTime);
//            userMapper.updateIntegralOneByUserId(userId);
//            return ResponseEntity.ok(" obtain browse integral successfully.");
//        } else {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("You have already obtain browse integral today.");
//        }
//    }


}




